//
//  SignatureVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/7.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "SignatureVC.h"
#import "MicroDef.h"
#import "JKAlertDialog.h"
#import "CHSCharacterCountTextView.h"
#import "CXmppMgr.h"
#import "CContactMgr.h"
#import "CXmppRequestMgr.h"
#import "UIUtil.h"
#import "MBProgressHUD+WZ.h"
#import "UIBarButtonItem+category.h"

@interface SignatureVC () <CHSCharacterCountTextViewDelegate>
{
    CHSCharacterCountTextView   *_textView;
}
@end

@implementation SignatureVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"个人签名"];
    self.curUser = [[[CContactMgr alloc] init] loadCurContact];

    self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"call_back" title:@"返回" target:self sel:@selector(back) titleFont:Font18 titleColor:WhiteColor];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:nil title:@"保存" target:self sel:@selector(save) titleFont:Font18 titleColor:WhiteColor];
    
//    _textView = [[CHSCharacterCountTextView alloc] initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH - 20, 140)];
//    _textView.center = CGPointMake(HARDWARE_SCREEN_WIDTH/2, _textView.frame.size.height/2+10);
//    [_textView setBackgroundColor:ClearColor];
//    _textView.delegate = self;
//    UIImageView* textBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _textView.frame.size.width+16, _textView.frame.size.height+10)];
//    [textBG setCenter:_textView.center];
//    CGRect fra = _textView.frame;
//    [textBG setFrame:fra];
//    [textBG setImage:[[UIImage imageNamed:@"searchcontentbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch]];
//    [self.view addSubview:textBG];
    _textView = [[CHSCharacterCountTextView alloc] init];
    _textView.layer.borderWidth = 0.5f;
    _textView.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    _textView.layer.cornerRadius = 2.5f;
    _textView.delegate = self;
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.userInteractionEnabled = YES;
    [self.view addSubview:_textView];
    
    if (self.curUser.Autograph.length > 0) {
        _textView.text = self.curUser.Autograph;
    }
    UIView *superView = self.view;
    
    CGFloat padding = 10.0f;
    CGFloat hight = 200.0f;
    
    _textView.frame = CGRectMake(padding, padding, HARDWARE_SCREEN_WIDTH - 2 * padding, hight);
    
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSuccess:) name:K_XMPPUPDATESIGNATURE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveFailed) name:K_XMPPDIDFAILEDRECEIVEIQ object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDIDFAILEDRECEIVEIQ object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPUPDATESIGNATURE object:nil];
}


#pragma mark 通知中心方法
- (void)saveSuccess:(NSNotification *)notice
{
    [UIUtil hideMBProgressViewForView:self.view];
    self.curUser.Autograph = _textView.text;
    [[[CContactMgr alloc] init] updateContact:self.curUser];
    __block SignatureVC *block = self;
    
    [MBProgressHUD showSuccess:@"保存成功"];
    [block.navigationController popViewControllerAnimated:YES ];
    
}

- (void)saveFailed
{
    [UIUtil hideMBProgressViewForView:self.view];
    [MBProgressHUD showError:@"保存失败，请您检查网络"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_textView endEditing:YES];
}
-(void)save
{
    [_textView endEditing:YES];
    if (_textView.text.length <= 0) {
       _textView.text = @"";
    }
    
    [self updateSignature];
    
    [UIUtil addMBProgressViewTitle:@"正在保存..." inView:self.view alpha:0.5 failureTitle:@"保存失败，请检查网络"];
}


-(void)back
{
    
    //    switch (self.vc_type.integerValue) {
    //        case 0:
    //        {
    //            if ([self.curUser.EXtenSion isEqualToString:_inputTX.text]) {
    //                [self.navigationController popViewControllerAnimated:YES ];
    //            } else {
    //
    //            }
    //        }
    //            break;
    //        case 1:
    //        {
    //            if ([self.curUser.MobileTelephone isEqualToString:_inputTX.text]) {
    //                [self.navigationController popViewControllerAnimated:YES ];
    //            } else {
    //
    //            }
    //        }
    //            break;
    //        case 2:
    //        {
    //            if ([self.curUser.EXtenSion isEqualToString:_inputTX.text]) {
    //                [self.navigationController popViewControllerAnimated:YES ];
    //            } else {
    //                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您修改的信息是否需要保存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    //                [alert show];
    //            }
    //
    //        }
    //            break;
    //
    //        default:
    //            break;
    //    }
    
    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您修改的信息是否需要保存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    //    [alert show];
    
    [_textView endEditing:YES];
    
    
        if ([self.curUser.Autograph isEqualToString:_textView.text]) {
            [self.navigationController popViewControllerAnimated:YES];
        } else{
            if (_textView.text==nil) {
               [self.navigationController popViewControllerAnimated:YES];
            }else{
                __block SignatureVC *block = self;
                JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:nil message:@"您修改的信息是否需要保存?"];
                [alert addButton:Button_OK withTitle:@"是" handler:^(JKAlertDialogItem *item) {
                    
                    [self save];
                }];
                
                [alert addButton:Button_CANCEL withTitle:@"否" handler:^(JKAlertDialogItem *item) {
                    
                    [block.navigationController popViewControllerAnimated:YES ];
                }];
                [alert show];

           
            }
            
        }
    
}

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    switch (buttonIndex) {
//        case 0:
//        {
//            [self.navigationController popViewControllerAnimated:YES ];
//        }
//            break;
//        case 1:
//        {
//            [self save];
//        }
//            break;
//        default:
//            break;
//    }
//}


#pragma mark 通知中心方法

-(void)updateSignature
{
    CContactModel *contact = [[[CContactMgr alloc]init] loadCurContact];
    [[CXmppRequestMgr shareInstance] updateSignatureWithAurograph:_textView.text AndCompanyId:contact.compCode];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark CHSCharacterCountTextViewDelegate
//bug 8679 fixed by lixu 9467 fixed
//-(void)characterCountTextViewDidChanged:(CHSCharacterCountTextView *)textView
//{
//    if (textView.text.length == 0) {
//        self.navigationItem.rightBarButtonItem.enabled = NO;
//    }
//    else
//    {
//        self.navigationItem.rightBarButtonItem.enabled = YES;
//    }
//}
//-(void)characterCountTextViewDidBeginEditing:(CHSCharacterCountTextView *)textView
//{
//    if (textView.text.length == 0) {
//        self.navigationItem.rightBarButtonItem.enabled = NO;
//    }
//    else
//    {
//        self.navigationItem.rightBarButtonItem.enabled = YES;
//    }
//}

@end
